package com.zadtek.sharkRider.activity;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.zadtek.sharkRider.R;

import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.RadioGroup.OnCheckedChangeListener;

/**
 * 排行榜
 * @author kuyukeji
 *
 */
public class PaihFragment extends FragmentActivity {

	private ImageView backid;
	private TextView topview; // 标题
	public TextView rightname;	//右边显示的月份

	private RadioGroup radioGroup;
	private FragmentTransaction fragmentTrans;
	
	private RadioButton radio_nopay;	//配送评分

	private PaihChildFragment fragment;	//排行
	
	private String tostate = "";
	private String totime = "";	//从我的排行过来的
	private int year = 0, month =0 ,day =0;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.fragment_paih);
		
		radio_nopay = (RadioButton) findViewById(R.id.radio_nopay);
		try {
			String value = getIntent().getStringExtra("tostate");
			if(value!=null && !value.equals("")) {
				tostate = value;
			}
			String timevalue = getIntent().getStringExtra("totime");
			if(timevalue!=null && !timevalue.equals("")) {
				totime = timevalue;
			}
		} catch(Exception e) {}
		
		backid = (ImageView) findViewById(R.id.backid);
		backid.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				finish();
			}
		});
		topview = (TextView) findViewById(R.id.topview);
		topview.setText("排行榜");
		rightname = (TextView) findViewById(R.id.rightname);
		rightname.setVisibility(View.VISIBLE);
		
		if(totime!=null && !totime.equals("")) {
			StringBuilder timesb = new StringBuilder(totime);
			timesb.insert(4, "年");
			try {
				year = Integer.parseInt(totime.substring(0, 4));
				month = Integer.parseInt(totime.substring(4, 6));
			} catch(Exception e){}
			if(totime.length() >6) {
				timesb.insert(7, "月");
				try {
					day = Integer.parseInt(totime.substring(6));
				} catch(Exception e){}
			} else {
				timesb.append("月");
				Calendar c = Calendar.getInstance();
				day = c.get(Calendar.DAY_OF_MONTH);
			}
			rightname.setText(timesb);
		} else {
			Calendar c = Calendar.getInstance();
			year = c.get(Calendar.YEAR);
			month = (c.get(Calendar.MONTH)+1);
			day = c.get(Calendar.DAY_OF_MONTH);
			rightname.setText(year+"年"+month+"月"+day);
		}
		
		rightname.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DatePickerDialog mydialog = new DatePickerDialog(PaihFragment.this,
						new DatePickerDialog.OnDateSetListener() {
							@Override
							public void onDateSet(DatePicker view, int year,
									int monthOfYear, int dayOfMonth) {
								String value = String.format("%d%d", year,
										monthOfYear+1);
								try {
									DateFormat df = new SimpleDateFormat("yyyyMM");
									String tostr = df.format(df.parse(value));
									//调整到对应的
									totime = tostr;
									if(radio_nopay.isChecked()) {	//是否被选中
										fragment = new PaihChildFragment();
										fragment.setState("1");
										fragment.setTime(totime);
										getSupportFragmentManager().beginTransaction()
												.replace(R.id.layout_content, fragment).commit();
									} else {
										radio_nopay.setChecked(true);
									}
									
									StringBuilder timesb = new StringBuilder(tostr);
									timesb.insert(4, "年");
									timesb.append("月");
									rightname.setText(timesb);
									
									year = Integer.parseInt(totime.substring(0, 4));
									month = Integer.parseInt(totime.substring(4, 6));
									Calendar c = Calendar.getInstance();
									day = c.get(Calendar.DAY_OF_MONTH);
									
								} catch(Exception e){}
							}
						}, year, month-1, day);
				mydialog.show();

				DatePicker dp = findDatePicker((ViewGroup) mydialog.getWindow()
						.getDecorView());
				if (dp != null) {
					((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0))
							.getChildAt(2).setVisibility(View.GONE);
				}
			}
		});

		radioGroup = (RadioGroup) findViewById(R.id.tab_radio);
		
		fragmentTrans = getSupportFragmentManager().beginTransaction();
		if (fragment == null) {	//默认显示第一个
			fragment = new PaihChildFragment(); 
			if(tostate!=null && !tostate.equals("")) {
				fragment.setState(tostate);
				if(tostate.equals("1"))
					radio_nopay.setChecked(true);
			}
			if(totime!=null && !totime.equals("")) {
				fragment.setTime(totime);
			}
			fragmentTrans.add(R.id.layout_content, fragment).commit();
        }

		radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				switch (checkedId) {
				case R.id.radio_all: // 配送准时率
					fragment = new PaihChildFragment();
					fragment.setState("3");
					if(totime!=null && !totime.equals("")) {
						fragment.setTime(totime);
					}
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;
					
				case R.id.radio_nopay: // 配送评分
					fragment = new PaihChildFragment();
					fragment.setState("1");
					if(totime!=null && !totime.equals("")) {
						fragment.setTime(totime);
					}
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;
					
				case R.id.radio_payed: // 配送单量
					fragment = new PaihChildFragment();
					fragment.setState("2");
					if(totime!=null && !totime.equals("")) {
						fragment.setTime(totime);
					}
					getSupportFragmentManager().beginTransaction()
							.replace(R.id.layout_content, fragment).commit();
					break;
				}
			}
		});
	}
	
	private DatePicker findDatePicker(ViewGroup group) {  
        if (group != null) {  
            for (int i = 0, j = group.getChildCount(); i < j; i++) {  
                View child = group.getChildAt(i);  
                if (child instanceof DatePicker) {  
                    return (DatePicker) child;  
                } else if (child instanceof ViewGroup) {  
                    DatePicker result = findDatePicker((ViewGroup) child);  
                    if (result != null)  
                        return result;  
                }  
            }  
        }  
        return null;  
    } 

}
